<?php
require_once('inc.config.php');

$linker = new linker();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My Domains and Applications</title>
<link href="css/list.css" rel="stylesheet" type="text/css" />
<base href="<?php echo $homepage; ?>" />
<style type="text/css">
<?php
require_once(__MYSELF__.'/css/list-css-embed.php');
?>
</style>
</head>
<body>
<div class="wrapper">
  <h1>Quick shortcuts:</h1>
  <ul>
<?php
if(!IS_MYSQL)
{
	echo '<li>Enable MySQL first.</li>';
}
else
{
$domain_sql="
SELECT
	d.domain_id,
	d.domain_name `name`,
	d.url_local `url`,
	d.is_live,
	d.is_valid,
	d.do_validate,
	(UNIX_TIMESTAMP(CURRENT_TIMESTAMP()) - checked_on) `diff`
FROM localhost_domains d
WHERE
	d.is_active='Y'
	AND is_local='Y'
ORDER BY
	d.domain_name
;";

$rs = mysql_query($domain_sql, $connection);
$specials_counter = 0;
if(mysql_num_rows($rs))
while($domain = mysql_fetch_assoc($rs))
{
	if($domain['diff'] >= $seconds)
	{
		# Check URLs marked for validation or that were checked in too old time.
		# This should superseed any other configurations.
		$domain['name'] = recheck_url($domain['url'], $domain['domain_id'], $domain['name']);
	}
	else if($domain['do_validate']=='Y')
	{
		$domain['name'] = recheck_url($domain['url'], $domain['domain_id'], $domain['name']);
	}
	else
	{
		if($domain['is_valid']!='Y')
		{
			# No need to check the URL again, we know, it is invalid.
			# Here, locally unapproved links will be cheked too frequently.
			#$domain['name'] = recheck_url($domain['url'], $domain['domain_id'], $domain['name']);
			$domain['name'] = "<del><span>{$domain['name']}</span></del>";
		}
		else
		{
			# In this case:
			# It was recently checked.
			# The URL is valid / existing.
			# There is no manual request made to recheck the URL.
			
			# Or, with do_validate=N and is_valid=Y flag, it is assumed that everything is okay.
		}
	}
	++$specials_counter;
	echo "<li class=\"specials\"><a href='{$domain['url']}'>{$domain['name']}</a></li>";
}

if(!$specials_counter)
{
	echo "<li class=\"error\">No links in database.</li>";
}

} # if(IS_MYSQL)
?>
  </ul>
  <div class="clear"></div>
  <h1>Live projects:</h1>
  <ul>
<?php
if(!IS_MYSQL)
{
	echo '<li>Enable MySQL first.</li>';
}
else
{
$domain_sql="
SELECT
	d.domain_id,
	d.domain_name `name`,
	d.url_live `url`,
	d.is_live,
	d.is_valid,
	d.do_validate,
	(UNIX_TIMESTAMP(CURRENT_TIMESTAMP()) - checked_on) `diff`
FROM localhost_domains d
WHERE
	d.is_active='Y'
	AND is_live='Y'
ORDER BY
	d.domain_name
;";
$rs = @mysql_query($domain_sql, $connection);
$specials_counter = 0;
if(@mysql_num_rows($rs))
while($domain = mysql_fetch_assoc($rs))
{
	++$specials_counter;
	echo "<li class=\"specials\"><a href='{$domain['url']}'>{$domain['name']}</a></li>";
}

if(!$specials_counter)
{
	echo "<li class=\"error\">No live projects yet.</li>";
}

} # if(!IS_MYSQL)
?>
  </ul>
  <div class="clear"></div>
  <h1>Local Folders:</h1>
  <ul>
<?php
# To avoid this application itself from the list.
$myself = basename(dirname(__FILE__));

$folders_counter = 0;
if ($handle = opendir($lookup_dir))
{
	while (false !== ($file = readdir($handle)))
	{
		$physical_file = $lookup_dir.'/'.$file;
		if(!is_dir($physical_file) || $file=='.' || $file=='..' || $file==$myself)
		{
			continue;
		}
		
		if($link = $linker->regular_directory($physical_file, $file))
		{
			++$folders_counter;
			echo $link;
		}
	}
	closedir($handle);
}
else
{
}

if(!$folders_counter)
{
	echo "<li class=\"error\">Local folders not found.</li>";
}
?>
  </ul>
  <div class="clear"></div>
  <h1>Regular PHP Files:</h1>
  <ul>
<?php
$files_counter = 0;
if ($handle = opendir($lookup_dir))
{
	while (false !== ($file = readdir($handle)))
	{
		$physical_file = $lookup_dir.'/'.$file;
		if(!is_file($physical_file) || $file=='index.php')
		{
			continue;
		}
		
		# Use PHP only filter
		if(!preg_match('/\.php$/', $file))
		{
			continue;
		}
		
		echo $linker->regular_file($file);
		
		++$files_counter;
	}
	closedir($handle);
}
else
{
}

if(!$files_counter)
{
	echo "<li class=\"error\">No regular PHP files.</li>";
}
?>
  </ul>
  <div class="clear"></div>
  <div class="footer">
    <p><?php echo $company; ?></p>
  </div>
	<h1>Color Legends:</h1>
	<ul>
		<li class="specials">Linked from database</li>
		<li class="specials"><a href='#'><del><span>Database link is down</span></del></a></li>
		<li class="active">Directory has <em>index.php</em></li>
		<li class="active">Or, <em>auto-listing.php</em></li>
		<li class="inactive">missing <em>index.php</em></li>
		<li class="inactive">Or, directory only</li>
		<li class="modified">Renamed via <em><a href="<?php echo basename(__MYSELF__); ?>/info/info.xml">info.xml</a></em></li>
		<li class="file">Regular PHP File</li>
		<li class="error">Error in resource</li>
	</ul>
	<div class="clear"></div>
</div>
</body>
</html>